
package product

import (
	"context"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/product"
    productReq "github.com/flipped-aurora/gin-vue-admin/server/model/product/request"
)

type ProductService struct {}
// CreateProduct 创建商品管理记录
// Author [yourname](https://github.com/yourname)
func (productsService *ProductService) CreateProduct(ctx context.Context, products *product.Product) (err error) {
	err = global.GVA_DB.Create(products).Error
	return err
}

// DeleteProduct 删除商品管理记录
// Author [yourname](https://github.com/yourname)
func (productsService *ProductService)DeleteProduct(ctx context.Context, productId string) (err error) {
	err = global.GVA_DB.Delete(&product.Product{},"product_id = ?",productId).Error
	return err
}

// DeleteProductByIds 批量删除商品管理记录
// Author [yourname](https://github.com/yourname)
func (productsService *ProductService)DeleteProductByIds(ctx context.Context, productIds []string) (err error) {
	err = global.GVA_DB.Delete(&[]product.Product{},"product_id in ?",productIds).Error
	return err
}

// UpdateProduct 更新商品管理记录
// Author [yourname](https://github.com/yourname)
func (productsService *ProductService)UpdateProduct(ctx context.Context, products product.Product) (err error) {
	err = global.GVA_DB.Model(&product.Product{}).Where("product_id = ?",products.ProductId).Updates(&products).Error
	return err
}

// GetProduct 根据productId获取商品管理记录
// Author [yourname](https://github.com/yourname)
func (productsService *ProductService)GetProduct(ctx context.Context, productId string) (products product.Product, err error) {
	err = global.GVA_DB.Where("product_id = ?", productId).First(&products).Error
	return
}
// GetProductInfoList 分页获取商品管理记录
// Author [yourname](https://github.com/yourname)
func (productsService *ProductService)GetProductInfoList(ctx context.Context, info productReq.ProductSearch) (list []product.Product, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
    // 创建db
	db := global.GVA_DB.Model(&product.Product{})
    var productss []product.Product
    // 如果有条件搜索 下方会自动创建搜索语句
    
	err = db.Count(&total).Error
	if err!=nil {
    	return
    }

	if limit != 0 {
       db = db.Limit(limit).Offset(offset)
    }

	err = db.Find(&productss).Error
	return  productss, total, err
}
func (productsService *ProductService)GetProductPublic(ctx context.Context) {
    // 此方法为获取数据源定义的数据
    // 请自行实现
}
